package ch06.symtab.nested;

import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;

import java.io.IOException;

public class Test {
    public static void main(String[] args) throws IOException, RecognitionException {
        CharStream input = null;
        if (args.length > 0) {
            input = new ANTLRFileStream(args[0]);
        } else {
            input = new ANTLRInputStream(System.in);
        }
        CymbolLexer lexer = new CymbolLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        CymbolParser p = new CymbolParser(tokens);
        RuleReturnScope r = p.compilationUnit();

        CommonTree t = (CommonTree)r.getTree();
        CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
        nodes.setTokenStream(tokens);
        SymbolTable symtab = new SymbolTable();
        DefRef def = new DefRef(nodes, symtab);
        def.downup(t);
        System.out.println("globals: " + symtab.globals);
    }
}
